/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDAtribuirTecnico.java
 *
 * Created on 09/01/2012, 11:37:15
 */
package view.admin;

import control.ChamadoControl;
import java.awt.Frame;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import model.Chamado;
import model.Pessoa;

/**
 *
 * @author Felipe
 */
public class JDAtribuirTecnico extends JDialog {

    private Pessoa usuarioLogado;
    private ChamadoControl controller;
    private Chamado chamado;
    
    /** Creates new form JDAtribuirTecnico */
    public JDAtribuirTecnico(Frame parent, boolean modal, Pessoa usuarioLogado,
                            Chamado chamado) {
        super(parent, modal);
        this.usuarioLogado = usuarioLogado;
        this.chamado = chamado;
        this.controller = ChamadoControl.getControl();
        initComponents();
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlGeral = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Atribuição de Técnico a chamado");

        pnlGeral.setLayout(new java.awt.BorderLayout());

        jTable1.setModel(controller.getTecnicosTableModel());
        jTable1.setRowHeight(24);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        pnlGeral.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(pnlGeral, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-308)/2, (screenSize.height-434)/2, 308, 434);
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount() == 2){
            atribuirChamadoATecnico();
        }
    }//GEN-LAST:event_jTable1MouseClicked

    public void atribuirChamadoATecnico() {
        Pessoa tecnico = controller.getTecnicosTableModel().getTecnico(getLinha());
        try {
            controller.atribuirChamadoATecnico(this.chamado, tecnico);
            JOptionPane.showMessageDialog(null, "Técnico atribuído com sucesso!");
            this.dispose();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            Logger.getLogger(JDAtribuirTecnico.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
    
    private int getLinha() {
        return jTable1.convertRowIndexToModel(jTable1.getSelectedRow());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JPanel pnlGeral;
    // End of variables declaration//GEN-END:variables
}
